<template>
	<view>
		<!-- 2书写轮播图的结构代码 -->
		<swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000">
			<swiper-item v-for="item,index in swiperData">
				<view class="swiper-item">
					<image :src="'http://localhost:8090/foodapp'+item.icon" mode="" style="width: 100%;"></image>
				</view>
			</swiper-item>
		</swiper>
		<!-- 2. 写宫格展示结构代码 -->
		<uni-section title="菜品类别" sub-title="" type="square">
			<uni-grid :column="4">
				<uni-grid-item v-for="item,index in gridData">
					<view class="grid-item" @click="toFoodList(item.typename)">
						<image :src="'http://localhost:8090/foodapp'+item.icon" mode=""
							style="width: 60px;height: 60px;"></image>
						<view>{{item.typename}}</view>
					</view>
				</uni-grid-item>
			</uni-grid>
		</uni-section>
		<uni-section title="新闻列表" sub-title="" @click="toNewsList" type="square">
			<uni-list>
				<uni-list-item v-for="item,index in newsData" :key="index+''" 
				:title="item.title" :clickable="true" :rightText="item.date"
				@click="toNewsDetail(item.id)"
				></uni-list-item>
			</uni-list>
		</uni-section>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				//1. 定义新闻的数据
				newsData: [{
					"id": 1,
					"title": "军事新闻1",
					"content": "军事新闻内容",
					"type": "军事",
					"date": "2024-03-01"
				}],
				// 1 定义轮播图的数据
				swiperData: [{
					"id": 1,
					"name": "烤鸭",
					"label": "烤鸭烤鸭烤鸭",
					"icon": "/images/1.png",
					"price": 88,
					"description": "烤鸭烤鸭烤鸭烤鸭烤鸭",
					"type": "肉类"
				}],
				// 1 定义宫格展示的数据
				gridData: [{
					"id": 1,
					"typename": "鱼类",
					"icon": "/images/fish.png"
				}]
			}
		},
		methods: {
			toNewsList(){
				uni.navigateTo({
					url:'/pages/news/newsList'
				})
			},
			toNewsDetail(id){
				uni.navigateTo({
					url:'/pages/news/newsDetail?id='+id
				})
			},
			toFoodList(typename) {
				// 转到foodList页面

				// 保存选中的食物类别信息到本地存储
				uni.setStorageSync('typename', typename)
				uni.switchTab({
					url: '/pages/foodlist/foodlist',
				})
			}
		},
		onLoad() {
			//加载数据
			// 3. 加载新闻数据
			uni.request({
				url: 'http://localhost:8090/foodapp/news/list',
				method: 'POST',
				data: {},
				success: res => {
					// 如果正确返回，则将data赋值给newsData
					if (res.data.success) {
						this.newsData = res.data.data
					} else{
						this.newsData=[]
					}
				},
				fail: () => {
					uni.showToast({
						title: '网络失败',
						icon: 'none'
					});
				},
				complete: () => {}
			});
			// 3. 编写宫格展示的数据加载代码
			uni.request({
				url: 'http://localhost:8090/foodapp/types/list',
				method: 'POST',
				data: {},
				success: res => {
					if (res.data.success) {
						this.gridData = res.data.data
					} else {
						this.gridData = []
					}
				},
				fail: () => {
					uni.showToast({
						title: '网络失败',
						icon: 'none'
					});
				},
				complete: () => {}
			});
			// 3. 编写轮播图的数据加载
			uni.request({
				url: 'http://localhost:8090/foodapp/food/list',
				method: 'POST',
				data: {},
				success: res => {
					if (res.data.success) {
						this.swiperData = res.data.data
						uni.showToast({
							title: '成功'
						});
					} else {
						uni.showToast({
							title: '失败',
							icon: 'none'
						});
					}
				},
				fail: () => {
					uni.showToast({
						title: '网络错误',
						icon: 'none'
					});
				},
				complete: () => {}
			});
		}
	}
</script>

<style>
	.grid-item {
		display: flex;
		flex-direction: column;
		align-items: center; // 子元素在父容器的交叉轴上居中
	}
</style>